package com.dxp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxp.common.CustomException;
import com.dxp.dto.SetmealDto;
import com.dxp.mapper.SetmealMapper;
import com.dxp.pojo.Category;
import com.dxp.pojo.Setmeal;
import com.dxp.pojo.SetmealDish;
import com.dxp.service.CategoryService;
import com.dxp.service.SetmealDishService;
import com.dxp.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    SetmealService setmealService;
    @Autowired
    CategoryService categoryService;
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //先插入基本的信息
        this.save(setmealDto);
        //操作setmeal_dish表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
    @Override
    public Page listSetMeal(int page, int pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealdtoPage = new Page<>();
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.like(StringUtils.isNotEmpty(name),"name",name);
        setmealQueryWrapper.orderByDesc("update_time");
        setmealService.page(setmealPage,setmealQueryWrapper);
        BeanUtils.copyProperties(setmealPage,setmealdtoPage," records");
        List<Setmeal> setmealPageRecords = setmealPage.getRecords();
        List<SetmealDto> setmealDtoPageRecords=setmealPageRecords.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealdtoPage.setRecords(setmealDtoPageRecords);
        return setmealdtoPage;
    }
    @Override
    public void deleteWithDish(List<Long> ids) {
        //先判断是否在售
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.in("id",ids);
        setmealQueryWrapper.eq("status",1);
        int count = this.count(setmealQueryWrapper);
        //还在售卖的话抛异常
        if(count>0){
            throw new CustomException("套餐还在售卖,无法删除");
        }
        //停售的话删除,先删除套餐 setmeal表
        setmealService.removeByIds(ids);
        //再删除对应的菜品信息,setmeal_dish表
        QueryWrapper<SetmealDish> setmealDishQueryWrapper = new QueryWrapper<>();
        setmealDishQueryWrapper.in("setmeal_id",ids);
        setmealDishService.remove(setmealDishQueryWrapper);
    }

    @Override
    public SetmealDto getByIdUpdateSetmeal(Long id) {
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        QueryWrapper<SetmealDish> setmealDishQueryWrapper = new QueryWrapper<>();
        setmealDishQueryWrapper.eq("setmeal_id",id);
        List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishQueryWrapper);
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }
    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        //先修改基本的信息
        this.updateById(setmealDto);
        //套餐对应的菜品，可以先删除再添加
        QueryWrapper<SetmealDish> setmealDishQueryWrapper = new QueryWrapper<>();
        setmealDishQueryWrapper.eq("setmeal_id",setmealDto.getId());
        setmealDishService.remove(setmealDishQueryWrapper);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
